<?php

session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

$user_id	= $_SESSION['uid'];
$user_name	= $_SESSION['unm'];
$mod		= ( ! empty($_GET['mod'])) ? (int)$_GET['mod'] : 0;
$abalance	= 0;
$msg_box	= '';

if ($r1 = $db -> query("SELECT * FROM bank WHERE user_id = '$user_id'"))
{
		
	$c = $r1 -> num_rows;
	
	// Se il conto � esistente, lo mostra
	if ($c == 1)
	{
		$rd1 = $r1 -> fetch_assoc();
		
		$abalance = $rd1['account_balance'];
		
		if ($r2 = $db -> query("SELECT user_money FROM users WHERE user_id = '$user_id'"))
		{
			$rd2 = $r2 -> fetch_assoc();
			
			$user_money = $rd2['user_money'];
			
			$r2 -> close();
		}
		$content = '<ul class="list-group">';
		$content .= '<li class="list-group-item"><b>Soldi in tasca</b> <span class="label label-primary pull-right">' . number_format($user_money, 0, ',', '.') . ' <b><small>' . $g_currency . '</small></b></span></li>';
		$content .= '<li class="list-group-item"><b>Bilancio</b> <span class="label label-primary pull-right">' . number_format($abalance, 0, ',', '.') . ' <b><small>' . $g_currency . '</small></b></span></li>';
		$content .= '</ul>';		
	
		$content .= '<form action="bank-execute.php" class="col-md-6" method="post" role="form">';
		$content .= '<h4>Deposita</h4>';
		$content .= '<input type="hidden" name="action" value="2" />';
		$content .= '<div class="form-group"><input type="text" class="form-control" name="money_num" /></div>';
		$content .= '<input type="submit" class="btn btn-success" value="Deposita" />';
		$content .= '</form>';
		
		$content .= '<form action="bank-execute.php" class="col-md-6" method="post" role="form">';
		$content .= '<h4>Ritira</h4>';
		$content .= '<input type="hidden" name="action" value="3" />';
		$content .= '<div class="form-group"><input type="text" class="form-control" name="money_num" /></div>';
		$content .= '<input type="submit" class="btn btn-success" value="Ritira" />';
		$content .= '</form>';
		
		$account_balance = $rd1['account_balance'];
		$account_id = $rd1['account_id'];
		
	} else {
		// Se non esiste, mostra il form per crearlo
		$content = '<div class="alert alert-warning">Conto Inesistente</div>';
		$content .= '<form action="bank-execute.php" method="post" role="form">';
		$content .= '<div class="well">Non c\'&egrave; nessun conto associato a questo personaggio.<br />';
		$content .= 'Con un versamento base di 50 ' . $g_currency . ' puoi aprirne uno.</div>';
		$content .= '<input type="hidden" name="action" value="1" />';
		$content .= '<input type="hidden" name="money_num" value="50" />';
		$content .= '<input type="submit" class="btn btn-success btn-large" value="Crea Conto" /></form>';
	}
}

switch ($mod)
{
	case 1:
		$msg_box .= '<div class="alert alert-success">';
		$msg_box .= 'Conto creato con successo.';
		$msg_box .= '</div>';
	break;
	case 2:
		$msg_box .= '<div class="alert alert-success">';
		$msg_box .= 'Quota depositata con successo.';
		$msg_box .= '</div>';
	break;
	case 3:
		$msg_box .= '<div class="alert alert-success">';
		$msg_box .= 'Quota ritirata con successo.';
		$msg_box .= '</div>';
	break;
	case 4:
		$msg_box .= '<div class="alert alert-warning">';
		$msg_box .= 'Versamento minimo insufficiente.';
		$msg_box .= '</div>';
	break;
	case 5:
		$msg_box .= '<div class="alert alert-warning">';
		$msg_box .= $g_currency.' insufficienti';
		$msg_box .= '</div>';
	break;
}

// Lista delle tag speciali
$tags = array(
				'temp_title' => $g_name, 'temp_currency' => $g_currency, 'temp_userid' => $user_id,
				'temp_username' => $user_name, 'temp_balance' => $abalance, 'temp_content' => $content,
				'temp_msgbox' => $msg_box
			);

// Carica il template e formatta le tag speciali
$temp = new template('style/bank.template.html');
$temp -> replace($tags);
	
// Visualizza l'output
echo $temp -> show();

// Chiude la connessione al database
$db -> close();

?>